<?php
/*
 * File:page.class.php
 * Created:2013-5-18
 * Author:oldjiang
 * 
 */
/**
 *
 * 效果是百度分页那样的,不涉及数据库操作,
 * 实际上,分页的确是和数据库操作分开的
 * 使用方法
 * $total = 50;
 * $page = new Page($total);
 * echo $page->show();
 */
class Page {
	public $total; //总页数,在这里,你需要把总页数算出来
	public $p; //当前页数
	public $start; //排序起始页
	public $end; //排序终止页
	public $url; //当前的url,例如 index.php?p=
	public $display; //左边显示的页数,例如是5,就是说左边有5页,总共是10页
	public $isPreNext;  //是否显示上一页和下一页
	public $isStartEnd; //是否显示首页和最后一页
	public function Page($page,$total, $url = '?p=', $display = '5' ,$isPreNext=true ,$isStartEnd=true) {
		$this->p = $page;
		$this->total = $total;
		$this->url = $url;
		$this->display = $display;
		$this->init();
		$this->order();
		$this -> isPreNext = $isPreNext;
		$this -> isStartEnd = $isStartEnd;
	}
	/**
	   *初始化,包括简单的安全处理
	   */
	public function init() {
		//获取当前的页数
		$this->p = ($this->p + 0 <= 0) ? 1 : ($this->p + 0);
		if (!is_int($this->p)) {
			$this->p = 1;
		}
		//如果有人在页面上输入一个较大的数,显示最后一页
		if ($this->p >= $this->total) {
			$this->p = $this->total;
		}
	}
	/**
	  *这里将是怎么显示为百度分页的那种效果
	  *
	  */
	public function order() {
		if ($this->total <= 2 * $this->display) {
			$this->start = 1;
			$this->end = $this->total;
		} else {
			if ($this->p <= $this->display) {
				$this->start = 1;
				$this->end = 2 * $this->display;
			} else {
				if ($this->p > $this->display && ($this->total - $this->p >= $this->display - 1)) {
					$this->start = $this->p - $this->display;
					$this->end = $this->p + $this->display - 1;
				} else {
					$this->start = $this->total - 2 * $this->display + 1;
					$this->end = $this->total;
				}
			}
		}
	}
	public function show() {
		//如果没有数据,当然也就没有分页了
		if ($this->total <= 0) {
			return false;
		}

		$re = '<div id="page" >';
		// $pre前一页 $next 后一页
		if($this -> isStartEnd) $re .= "<a href=\"{$this->url}1\">首页</a> ";
		$pre = ($this->p - 1 <= 0) ? 1 : ($this->p - 1);
		if($this -> isPreNext) $re .= "<a href=\"{$this->url}$pre\">前一页</a> ";
		//如果当前页是第一页,不要首页和前一页
		if ($this->p == 1)$re = '<div id="page" >';
		for ($i = $this->start; $i <= $this->end; $i++) {
			$re .= ($i == $this->p) ? "$i " : "<a href=\"{$this->url}$i\">[$i] </a> ";
		}

		$next = ($this->p + 1 >= $this->total) ? $this->total : ($this->p + 1);
		//当前页是最后一页的页数,不要下一页和最后一页
		if ($this->p != $this->total) {
			if($this -> isPreNext) $re .= "<a href=\"{$this->url}$next\">下一页</a> ";
			if($this -> isStartEnd) $re .= "<a href=\"{$this->url}$this->total\">最后一页</a>";
		}
		return $re;
	}
}
?>
